package com.leetcode;

/**
 * 3. 无重复字符的最长子串
 * 滑动窗口 + indexOf
 */
public class Solution3_2 {

    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        int l = 0;
        int r = -1;
        int length = 0;
        while (l < n) {
            if (r + 1 < n) {
                int idx = s.indexOf(s.charAt(r + 1), l);
                if (idx == -1 || idx > r) {
                    r++;
                } else {
                    l++;
                }
            } else {
                l++;
            }
            length = Math.max(length, r - l + 1);
        }
        return length;
    }

}
